package io.zcoder.innoway.models;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.zcoder.innoway.utils.Utils;

public class EntryMonth implements Parcelable {
    int month;
    int year;
    int entryPosition;
    int monthPosition;
    int unit;
    List<EntryTime> entryTimes;

    public EntryMonth() {
    }

    public EntryMonth(int entryPosition, int monthPosition, int unit, List<EntryTime> entryTimes) {
        this.entryPosition = entryPosition;
        this.monthPosition = monthPosition;
        this.unit = unit;
        this.entryTimes = entryTimes;
    }

    protected EntryMonth(Parcel in) {
        month = in.readInt();
        year = in.readInt();
        entryPosition = in.readInt();
        monthPosition = in.readInt();
        unit = in.readInt();
        entryTimes = in.createTypedArrayList(EntryTime.CREATOR);
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(month);
        dest.writeInt(year);
        dest.writeInt(entryPosition);
        dest.writeInt(monthPosition);
        dest.writeInt(unit);
        dest.writeTypedList(entryTimes);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<EntryMonth> CREATOR = new Creator<EntryMonth>() {
        @Override
        public EntryMonth createFromParcel(Parcel in) {
            return new EntryMonth(in);
        }

        @Override
        public EntryMonth[] newArray(int size) {
            return new EntryMonth[size];
        }
    };

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getEntryPosition() {
        return entryPosition;
    }

    public void setEntryPosition(int entryPosition) {
        this.entryPosition = entryPosition;
    }

    public int getMonthPosition() {
        return monthPosition;
    }

    public void setMonthPosition(int monthPosition) {
        this.monthPosition = monthPosition;
    }

    public int getUnit() {
        return unit;
    }

    public void setUnit(int unit) {
        this.unit = unit;
    }

    public List<EntryTime> getEntryTimes() {
        return entryTimes;
    }

    public void setEntryTimes(List<EntryTime> entryTimes) {
        this.entryTimes = entryTimes;
    }

    public String getStartDate(int pos) {
        if (pos < 0 || pos >= entryTimes.size()) {
            return "";
        }
        Date date = Utils.convertToDate(entryTimes.get(pos).getTime(), Utils.DATE_FORMAT_YYYY_MM);
        return Utils.convertToString(date, Utils.DATE_FORMAT_YYY_MM_DD);
    }

    public String getEndDate(int pos) {
        if (entryTimes == null || pos < 0 || pos >= entryTimes.size()) {
            return "";
        }
        Date date = Utils.convertToDate(entryTimes.get(pos).getTime(), Utils.DATE_FORMAT_YYYY_MM);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return Utils.convertToString(calendar.getTime(), Utils.DATE_FORMAT_YYY_MM_DD);
    }

    public boolean isBack() {
        return monthPosition > 0;
    }

    public boolean isNext() {
        return monthPosition < entryTimes.size() - 1;
    }

    public String getLabelTime() {
        if (entryTimes == null || entryTimes.size() == 0) {
            return "";
        }
        Date date = Utils.convertToDate(entryTimes.get(0).getTime(), Utils.DATE_FORMAT_YYYY_MM);
        return Utils.convertToString(date, Utils.DATE_FORMAT_MMMM_YYYY);
    }
}
